<?php
class Businessnature extends MY_Controller {
	
	var $currentPage = 'businessnature';
	var $caption = 'Businessnature';
	var $action = '';
	
	public function index($start=0,$limit=10) {
		
		$this->load->library('pagination');
		$config['base_url'] = base_url() . 'admin/businessnature/index/';
		$config['total_rows'] = count($this->mBusinessnature->getAllEntries());
		$config['uri_segment'] = 4;
		$config['per_page'] = $limit;
		$config['cur_page'] = $this->uri->segment(4);
		$config['use_page_numbers'] = true;
		$config['num_links'] = 5;
		$config['first_link'] = '<span class="page-far-left">';
		$config['last_link'] = '<span class="page-far-right">';
		
		$this->pagination->initialize($config);
	    $data['businessnaturelist'] = $this->mBusinessnature->limit($limit,(($start-1) * $limit));
	    $data['pagination'] = $this->pagination->create_links();
		
		$this->loadAdminPage($data);
		
	}
	
	public function create($data='') {
		$this->action = 'add';
		
		$this->loadAdminPage($data);
	}
	
	public function add() {
		$businessnature = $this->mBusinessnature;
		
		$businessnature->code = trim($this->input->post('code',TRUE));
		$businessnature->name = trim($this->input->post('name',TRUE));
		$businessnature->description = trim($this->input->post('description',TRUE));
		
		
		$this->message = INSERT_ERROR;
		
		if(strlen($businessnature->code) != 0 && strlen($businessnature->name) != 0) {
			
			if(count($this->mBusinessnature->find_by_code($businessnature->code))==0) {
				$this->message = INSERT_SUCCESS;
				$businessnature->insert();	
			} else {
				$this->message = HAS_DUPLICATE . " code.";
				
				$data['businessnature'] = $businessnature;
				
				$this->create($data);
				
				return;
			}
		}
		
		$this->index();
	}
	
	public function edit($id) {
		 
		$businessnature = $this->mBusinessnature->getEntryById($id);
		
		if(count($businessnature) == 0) {
			show_error("Invalid business nature", 500, "Invalid data");
		}
	
		$data['businessnature'] = $businessnature[0];
		
		$this->action = 'edit';
		$this->id = $businessnature[0]->id;
		$this->loadAdminPage($data);
	}
	
	public function update() {
		
		$businessnature = $this->mBusinessnature;
		
		$businessnature->id = $this->input->post('id',TRUE);
		$businessnature->code = trim($this->input->post('code',TRUE));
		$businessnature->name = trim($this->input->post('name',TRUE));
		$businessnature->description = trim($this->input->post('description',TRUE));
		
		$this->message = UPDATE_ERROR;
		
		if(strlen($businessnature->id) != 0 && strlen($businessnature->code) != 0 && strlen($businessnature->name) != 0) {
			
			if(count($this->mBusinessnature->find_by_code_of_others($businessnature->id,$businessnature->code))==0) {
				if($businessnature->update())
					$this->message = UPDATE_SUCCESS;
			} else {
				$this->message = HAS_DUPLICATE . " code.";
			}
		}
		
		$this->edit($businessnature->id);
	}
	
	public function delete($id) {
		
		$this->message = DELETE_ERROR;
		
		if($this->mBusinessnature->delete($id))
			$this->message = DELETE_SUCCESS;
		
		$this->index();
	}
}

?>